<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Index extends MY_Controller {
	function __construct() {
		parent::__construct();

		$this->load->model('user_model', '', TRUE);
		$this->load->model('association_model', '', TRUE);
		$this->load->model('society_model', '', TRUE);
		$this->load->model('news_model', '', TRUE);
		$this->load->model('announcement_model', '', TRUE);
	}

	public function index()
	{
		$condition['sort_view_count'] = DESC;
		$condition['offset'] = '';
		$condition['per_page'] = "5";
		$data['hot_society'] = $this->society_model->search_by_condition($condition);
		$data['current_menu_tab'] = MENU_TAB_A;

		//获得最新资讯幻灯图片
		$latest_news_pic_condition['sort_create_time'] = DESC;
		$latest_news_pic_condition['is_published'] = IS_PUBLISHED;
		$latest_news_pic_condition['offset'] = '';
		$latest_news_pic_condition['per_page'] = "20";
		$temp_pic_data = $this->news_model->search_by_condition($latest_news_pic_condition);
		if ($temp_pic_data != "") {
			foreach ($temp_pic_data as $k => $v) {
				$string = $v['contents'];
				$str_pattern = "/<img alt=\"\" src=\"(.*)\" \/>/";

				preg_match($str_pattern, $string, $temp_res[$k][]);

				$temp_res[$k]['id'] = $v['id'];
				$temp_res[$k]['title'] = $v['title'];
				$temp_res[$k]['category_id'] = $v['category_id'];
			}
				
			foreach ($temp_res as $k => $v) {
				if ($v[0] != null) {
					$result[] = array('id' => $v['id'], 'title' => $v['title'], 'category_id' => $v['category_id'], 'pic_path' => $temp_res[$k][0][1]);
				}
			}
			if (isset($result)) {
				$data['latest_news_pic'] = $result;
			}
		}

		//获得最新资讯
		$latest_news_condition['sort_create_time'] = DESC;
		$latest_news_condition['is_published'] = IS_PUBLISHED;
		$latest_news_condition['offset'] = '';
		$latest_news_condition['per_page'] = "3";
		$data['latest_news'] = $this->news_model->search_by_condition($latest_news_condition);
		if (!empty($data['latest_news'])) {
			foreach ($data['latest_news'] as $k => $v) {
				if ($v['category_id'] == SOCIETY_UCID) {
					$data['latest_news'][$k]['author_abbr'] = $this->society_model->_get_abbr_by_society_id($data['latest_news'][$k]['author_id']);
				}
			}
		}

		//获得新闻开始
		$condition['sort_create_time'] = DESC;
		$condition['is_published'] = IS_PUBLISHED;
		//获得社联新闻（cid == 2）
		$condition['category'] = ASSOCIATION_UCID;
		$data['association_news'] = $this->news_model->search_by_condition($condition);
		$data['association_news_count'] = $this->news_model->search_by_condition($condition, "", TRUE);

		//获得社团新闻（cid == 1）
		$condition['category'] = SOCIETY_UCID;
		$data['society_news'] = $this->news_model->search_by_condition($condition);
		$data['society_news_count'] = $this->news_model->search_by_condition($condition, "", TRUE);
		if (!empty($data['society_news'])) {
			foreach ($data['society_news'] as $k => $v) {
				$data['society_news'][$k]['author_abbr'] = $this->society_model->_get_abbr_by_society_id($data['society_news'][$k]['author_id']);
			}
		}
		
		//获得公告开始
		$condition['sort_create_time'] = DESC;
		$condition['is_published'] = IS_PUBLISHED;
		//获得社联公告（cid == 2）
		$condition['category'] = ASSOCIATION_UCID;
		$data['association_announcement'] = $this->announcement_model->search_by_condition($condition);
		
		//获得社团公告（cid == 1）
		$condition['category'] = SOCIETY_UCID;
		$data['society_announcement'] = $this->announcement_model->search_by_condition($condition);

		////////////前台已登陆用户信息绑定/////////////
		$data['user_info'] = $this->login_user_info();
		
		
		$data['html_title'] = MENU_TAB_A_NAME;
		$this->load->view('frontend/index', $data);
	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */